import random
import pygame
import sys
from pygame.locals import *
import time


class she(object):
    def __init__(self):
        self.Cell_W = int(800 / 20)
        self.Cell_H = int(500 / 20)

    def she1(self):
        startx = random.randint(5, self.Cell_W - 6)
        starty = random.randint(5, self.Cell_H - 6)
        wormCoords = [{'x': startx, 'y': starty},
                      {'x': startx - 1, 'y': starty},
                      {'x': startx - 2, 'y': starty}]
        return wormCoords

    def changdu(self, wormCoords):
        for coord in wormCoords:
            x = coord['x'] * 20
            y = coord['y'] * 20
            wormSegmentRect = pygame.Rect(x, y, 20, 20)
            pygame.draw.rect(screen, (0, 255, 0), wormSegmentRect)


class food(object):
    def __init__(self):
        self.Cell_W = int(800 / 20)
        self.Cell_H = int(500 / 20)

    def dou(self, coord):
        x = coord['x'] * 20
        y = coord['y'] * 20
        appleRect = (x, y, 20, 20)
        pygame.draw.rect(screen, (255, 0, 0), appleRect)

    def chi(self):
        return {'x': random.randint(0, self.Cell_W - 1), 'y': random.randint(0, self.Cell_H - 1)}


class fangxiang(object):
    def __init__(self, wormCoords, f):
        self.wormCoords = wormCoords
        self.direction = 'right'
        self.f = food()

    def fx(self):
        for event in pygame.event.get():
            if event.type == QUIT:
                pygame.quit()
                sys.exit()
            elif event.type == KEYDOWN:
                if (event.key == K_LEFT) and self.direction != 'right':
                    self.direction = 'left'
                elif (event.key == K_RIGHT) and self.direction != 'left':
                    self.direction = 'right'
                elif (event.key == K_UP) and self.direction != 'down':
                    self.direction = 'up'
                elif (event.key == K_DOWN) and self.direction != 'up':
                    self.direction = 'down'

    def gen(self):
        if self.direction == 'up':
            n = {'x': self.wormCoords[0]['x'], 'y': self.wormCoords[0]['y'] - 1}
        elif self.direction == 'down':
            n = {'x': self.wormCoords[0]['x'], 'y': self.wormCoords[0]['y'] + 1}
        elif self.direction == 'left':
            n = {'x': self.wormCoords[0]['x'] - 1, 'y': self.wormCoords[0]['y']}
        elif self.direction == 'right':
            n = {'x': self.wormCoords[0]['x'] + 1, 'y': self.wormCoords[0]['y']}
        self.wormCoords.insert(0, n)


def runGame():
    f = food()
    s = she()
    wormCoords = s.she1()
    fx = fangxiang(wormCoords, f)
    apple = f.chi()
    while True:
        fx.fx()
        if wormCoords[0]['x'] == -1 or wormCoords[0]['x'] == f.Cell_W or wormCoords[0]['y'] == -1 or \
                wormCoords[0]['y'] == f.Cell_H:
            return
        for i in wormCoords[1:]:
            if i['x'] == wormCoords[0]['x'] and i['y'] == wormCoords[0]['y']:
                return
        if wormCoords[0]['x'] == apple['x'] and wormCoords[0]['y'] == apple['y']:
            apple = f.chi()
        else:
            del wormCoords[-1]
        fx.gen()
        screen.fill((0, 0, 0))  # 初始化窗口，使得消除移动轨迹
        for x in range(0, 800, 20):
            pygame.draw.line(screen, (255, 255, 255), (x, 0), (x, 500))
        for y in range(0, 500, 20):
            pygame.draw.line(screen, (255, 255, 255), (0, y), (800, y))
        s.changdu(wormCoords)
        f.dou(apple)
        pygame.display.update()
        time.sleep(0.1)


def main():
    global screen
    screen = pygame.display.set_mode((800, 500))
    while True:
        runGame()


if __name__ == '__main__':
    main()
